﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kaixin.Components.DTO;
using System.Xml.Linq;
using System.IO;

namespace Kaixin.Components.XML
{
    /// <summary>
    /// 被监控用户账号的持久化操作类
    /// 创建：2009-12-9 14:54:21
    /// 版本：1.0
    /// 作者：吴鸣震
    /// </summary>
    public class UserSet
    {
        private string _fileName;

        private XDocument _xd;

        public UserSet()
        {
            _fileName = AppDomain.CurrentDomain.BaseDirectory + "USER.XML";
            if (File.Exists(_fileName))
            {
                _xd = XDocument.Load(_fileName);
            }
            else
            {
                _xd = new XDocument(new XElement("root"));
                _xd.Save(_fileName);
            }
        }

        public void Add(User user)
        {
            _xd.Element("root").Add(new XElement("user", new XElement("email", user.Email), new XElement("password", user.Password)));
            _xd.Save(_fileName);
        }
        /// <summary>
        /// 返回所有监控用户
        /// </summary>
        /// <returns></returns>
        public List<User> GetAllItem()
        {
            List<User> list = new List<User>();
            foreach (var item in _xd.Element("root").Elements("user"))
            {
                User user = new User(item.Element("email").Value, item.Element("password").Value);
                list.Add(user);
            }
            return list;
        }
    }
}
